package com.quasar.hpatient.model.controllermodel;

import com.quasar.hpatient.MainAppliaction;
import com.quasar.hpatient.constant.ConstantManger;
import com.quasar.hpatient.model.medicalmodel.PhotoUrlData;
import com.quasar.hpatient.network.RetrofitConnection;
import com.vise.log.ViseLog;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.Flowable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import top.zibin.luban.Luban;

/**
 * author : 许建宏
 * Date: 2017/10/10 0010
 * View:图片提交
 */

public class PictureConstant<T> {

    private updateUiInit uiInit;

    public PictureConstant(updateUiInit uiInit) {
        this.uiInit = uiInit;
    }

    private HashSet<String> joblist = null;

    public void PostPicture(List<File> picPath) {
        List<String> strings=new ArrayList<>();
        for(int i=0;i<picPath.size();i++){
            strings.add(picPath.get(i).getAbsolutePath());
        }

        Flowable.just(strings)
                .observeOn(Schedulers.io())
                .map(new Function<List<String>, List<File>>() {
                    @Override public List<File> apply(@NonNull List<String> list) throws Exception {
                        return Luban.with(MainAppliaction.getInstance()).load(list).get();
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<List<File>>() {
                    @Override public void accept(@NonNull List<File> list) throws Exception {
                        showResult(list);
                    }
                });
    }

    private void showResult(List<File> picPath) {

        joblist = new HashSet<String>();

        Map<String, RequestBody> partMap = new HashMap<>();

        for(int i=0;i<picPath.size();i++){
            RequestBody fileBody = RequestBody.create(MediaType.parse("image/jpeg"), picPath.get(i));
            partMap.put("file"+i+"\"; filename=\""+picPath.get(i).getName()+"\"", fileBody);
        }

        RequestBody dataBody = RequestBody.create(MediaType.parse("text/plain"), new String(ConstantManger.userType));
        partMap.put("type",dataBody);
        ViseLog.d("enter");
        RetrofitConnection.getBasicDataUrls()
                .PostPicture(partMap)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<PhotoUrlData>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {

                    }

                    @Override
                    public void onNext(@NonNull PhotoUrlData photoUrlData) {

                        ViseLog.d("onNext" + photoUrlData.getData().getFile());
                        if(photoUrlData.getData()!=null){
                            if (photoUrlData.getData().getFile().size() != 0) {
                                for (int i = 0; i < photoUrlData.getData().getFile().size(); i++) {
                                    joblist.add(photoUrlData.getData().getFile().get(i).getPhoto());
                                }
                            }
                        }

                        //将返回的图片字符串进行逗号分隔
                        if (joblist != null && joblist.size() != 0) {
                            String datas = "";
                            Iterator<String> iterator = joblist.iterator();
                            while (iterator.hasNext()) {
                                datas += iterator.next() + ",";
                            }
                            datas = datas.substring(0, datas.length() - 1);

                            uiInit.updateInit(datas);
                        }

                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        ViseLog.d("onError" + e.getMessage());
                        uiInit.onErrPhoto("图片提交失败");
                    }

                    @Override
                    public void onComplete() {
                        ViseLog.d("onComplete");
                    }
                });
    }


    public interface updateUiInit{
        void updateInit(String path);
        void onErrPhoto(String errVal);
    }

}
